<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="配置项" v-model="listQuery.f_lk_comment"></el-input>
      <el-button class="filter-item" type="success" icon="el-icon-search" @click="resetFiler">重置</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="数据正在加载中，请稍后..." border fit highlight-current-row>
      <el-table-column align="center" label='ID' width="80">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="配置项">
        <template slot-scope="scope">
          <span>{{scope.row.comment}}</span>
        </template>
      </el-table-column>
      <el-table-column label='类型'>
        <template slot-scope="scope">
          {{scope.row.type}}
        </template>
      </el-table-column>
      <el-table-column label='属性值'>
        <template slot-scope="scope">
          {{scope.row.value}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="left">
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="handleUpdateSettingEvent(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--修改对话框-->
    <el-dialog title="修改" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="setting" label-position="left" label-width="200px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="配置项" prop="comment">
          <el-input type="text" v-model="setting.comment" disabled></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input type="text" v-model="setting.type" disabled></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="value">
          <el-input type="text" v-model="setting.value" placeholder="请输入属性值"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="tryUpdateSetting">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSettingList, updateSetting } from '@/api/setting'

const typeMap = {
  'int': '整数',
  'long': '整数',
  'string': '字符串'
}
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        f_lk_comment: undefined,
        sort: '+id'
      },
      dialogFormVisible: false,
      rules: {
        value: [{ required: true, message: '属性值不能为空。', trigger: 'change' }]
      },
      setting: {
        id: 0,
        name: '',
        value: '',
        type: '',
        comment: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getSettingList(this.listQuery).then(response => {
        this.list = response.data
        for (const v of this.list) {
          v.type = typeMap[v.type] || v.type
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    resetFiler() {
      this.listQuery.f_lk_comment = undefined
      this.fetchData()
    },
    handleUpdateSettingEvent(row) {
      // 设置界面数据
      this.setting.id = row.id
      this.setting.name = row.name
      this.setting.type = row.type
      this.setting.value = row.value
      this.setting.comment = row.comment
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    tryUpdateSetting() {
      // 校验,然后执行
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const params = {}
          params.id = this.setting.id
          params.name = this.setting.name
          params.value = this.setting.value
          console.log(params)
          updateSetting(params).then(() => {
            this.dialogFormVisible = false
            this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
            this.fetchData()
          })
        }
      })
    }
  }
}
</script>
